package frontend

import (
	"blog/model"
	"blog/service/data"
	"net/http"

	"github.com/gin-gonic/gin"
)

// 显示添加页面
func AddLinkHandle(ctx *gin.Context) {
	type Data struct {
		PageName string
	}
	var D Data
	D.PageName = "友链申请"

	ctx.HTML(http.StatusOK, "addLink.html", D)
}

// 执行添加
func ToAddLinkHandle(ctx *gin.Context) {
	var lk model.LinkData
	//表单检查
	linkService := &data.LinkDataService{}
	err := linkService.ValidateLinkForm(ctx, &lk, ctx.Request.Form)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	//前台添加的不直接显示
	lk.IsShow = 0

	//执行添加
	err = linkService.CreateLink(ctx, &lk)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": "添加失败:" + err.Error()})
		return
	}

	//网页跳转
	ctx.JSON(http.StatusOK, gin.H{"message": "您的友情链接申请已经提交，请等待管理员审核"})
}
